package com.hch.maker.meta;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.hch.maker.meta.enums.FIleGenerateTypeEnum;
import com.hch.maker.meta.enums.FIleTypeEnum;
import com.hch.maker.meta.enums.ModelTypeEnum;

import java.io.File;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hch
 * @version 1.0
 * @description
 * @date 2024/7/27 16:02
 */

public class MetaValidator {

    public static void doValidAndFill(Meta meta){
        ValidData(meta);
        ValidFileConifgData(meta);
        ValidModelConfigData(meta);

    }

    private static void ValidModelConfigData(Meta meta) {
        Meta.ModelConfigDTO modelConfig = meta.getModelConfig();

        List<Meta.ModelConfigDTO.ModelsDTO> models = modelConfig.getModels();
        if(CollUtil.isNotEmpty(models)){
            for (Meta.ModelConfigDTO.ModelsDTO model : models) {
                String groupKey = model.getGroupKey();
                if(StrUtil.isNotEmpty(groupKey)){
                    String allArgs = model.getModels().stream().map((item) -> {
                        return String.format("\"--%s\"", item.getFieldName());
                    }).collect(Collectors.joining(", "));
                    model.setAllArgsStr(allArgs);
                    continue;
                }
                String fieldName = model.getFieldName();
                if(StrUtil.isBlank(fieldName)){
                    throw new MetaException("fieldName未填写");
                }
                String type = model.getType();
                if(StrUtil.isBlank(type)){
                    model.setType(ModelTypeEnum.STRING.getValue());
                }

            }
        }
    }

    private static void ValidFileConifgData(Meta meta) {
        Meta.FileConfigDTO fileConfig = meta.getFileConfig();
        if(fileConfig == null || fileConfig.getSourceRootPath() == null){
            return;
        }
        if(fileConfig != null){
            String sourceRootPath = fileConfig.getSourceRootPath();
            if(StrUtil.isBlank(sourceRootPath)){
                throw new MetaException("sourceRootPath 未填写");
            }
            String inputRootPath = fileConfig.getInputRootPath();
            if(StrUtil.isBlank(inputRootPath)){
                String defaultInputRootPath = ".source/"  + FileUtil.getLastPathEle(Paths.get(sourceRootPath)).getFileName().toString();
                fileConfig.setInputRootPath(defaultInputRootPath);
            }
            String outputRootPath = fileConfig.getOutputRootPath();
            String defaultOutputRootPath = "generated";
            if(StrUtil.isEmpty(outputRootPath)){
                fileConfig.setOutputRootPath(defaultOutputRootPath);
            }
            String type = fileConfig.getType();
            String defaultType = FIleTypeEnum.DIR.getValue();
            if(StrUtil.isEmpty(type)){
                fileConfig.setType(defaultType);
            }
            List<Meta.FileConfigDTO.FilesDTO> files = fileConfig.getFiles();
            if(CollUtil.isNotEmpty(files)){
                for (Meta.FileConfigDTO.FilesDTO file : files) {
                    String type1 = file.getType();
                    if(FIleTypeEnum.GROUP.getValue().equals(type1)){
                        continue;
                    }
                    String inputPath = file.getInputPath();
                    if (StrUtil.isEmpty(inputPath)) {
                        throw new MetaException("未输入inputPath路径");
                    }

                    String outputPath = file.getOutputPath();
                    if(StrUtil.isEmpty(outputPath)){
                        file.setOutputPath(inputPath);
                    }
                    String types = file.getType();
                    if (StrUtil.isBlank(types)) {
                        if(StrUtil.isBlank(FileUtil.getSuffix(inputPath))){
                            file.setType(FIleTypeEnum.DIR.getValue());
                        }else{
                            file.setType(FIleTypeEnum.FILE.getValue());
                        }
                    }
                    String generateType = file.getGenerateType();
                    if(StrUtil.isBlank(generateType)){
                        if(inputPath.endsWith(".ftl")){
                            file.setGenerateType(FIleGenerateTypeEnum.DYNAMIC.getValue());
                        }else{
                            file.setGenerateType(FIleGenerateTypeEnum.STATIC.getValue());
                        }
                    }

                }
            }

        }
    }

    private static void ValidData(Meta meta) {
        //基础信息校验
        String name = meta.getName();
        if(StrUtil.isBlank(name)){
            name = "my-project";
            meta.setName(name);
        }

        String description = meta.getDescription();
        if(StrUtil.isEmpty(description)){
            description = "我的代码模板";
            meta.setDescription(description);
        }

        String basePackage = meta.getBasePackage();
        if(StrUtil.isBlank(basePackage)){
            basePackage = "com.yupi";
            meta.setBasePackage(basePackage);
        }

        String version = meta.getVersion();
        if(StrUtil.isBlank(version)){
            version = "1.0";
            meta.setVersion(version);
        }

        String author = meta.getAuthor();
        if(StrUtil.isBlank(author)){
            author = "hch";
            meta.setAuthor(author);
        }
        String createTime = meta.getCreateTime();
        if(StrUtil.isBlank(createTime)){
            createTime = DateUtil.now();
            meta.setAuthor(createTime);
        }
    }
}
